package com.alibaba.citrus.ots.application.report.ability.repository.impl;

import com.alibaba.citrus.ots.application.report.ability.repository.MonthSalesStockReportQueryRepository;
import com.alibaba.cz.base.tool.page.PageResult;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.otsapplicationreportsdk.model.dto.MonthSalesStockReportLoadListRequest;
import com.epoch.app.otsapplicationreportsdk.model.dto.MonthSalesStockReportLoadListVO;
import com.epoch.app.otsapplicationreportsdk.model.dto.MonthSalesStockReportSDO;
import com.epoch.app.otsapplicationreportsdk.model.dto.QueryMonthStockReportRequest;
import com.epoch.app.otsapplicationreportsdk.monthsalesstockreport.model.MonthSalesStockReport;
import com.epoch.app.otsapplicationreportsdk.monthsalesstockreport.service.MonthSalesStockReportService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author zhoubc
 * @description: TODO
 * @date 2023/3/8 11:37
 */
@Component
public class MonthSalesStockReportQueryRepositoryImpl implements MonthSalesStockReportQueryRepository {

    @Resource
    MonthSalesStockReportService monthSalesStockReportService;

    @Override
    public PageResult<List<MonthSalesStockReportSDO>> pageQueryList(QueryMonthStockReportRequest request) {
        MonthSalesStockReportLoadListRequest loadListRequest = loadListRequestConverter(request);
        Result<List<MonthSalesStockReport>> response = monthSalesStockReportService.loadList(loadListRequest);
        List<MonthSalesStockReportSDO> result = response.getResult().stream().map(x -> {
            MonthSalesStockReportSDO bean = new MonthSalesStockReportSDO();
            BeanUtils.copyProperties(x,bean);
            return bean;
        }).collect(Collectors.toList());
        if(Objects.isNull(result)){
            return PageResult.of(new ArrayList<>(),0);
        }
        return PageResult.of(result,response.getTotal());
    }

    @Override
    public PageResult<List<MonthSalesStockReportSDO>> pageQueryMonthSalesStockReportList(QueryMonthStockReportRequest request) {
        MonthSalesStockReportLoadListVO listVO = loadListVORequestConverter(request);
        Result<List<MonthSalesStockReport>> response = monthSalesStockReportService.loadListForPageSort(listVO);
        List<MonthSalesStockReportSDO> result = response.getResult().stream().map(x -> {
            MonthSalesStockReportSDO bean = new MonthSalesStockReportSDO();
            BeanUtils.copyProperties(x,bean);
            if(StringUtils.isBlank(x.getScItemRootCategoryId())){
                bean.setScItemRootCategoryId("-1");
                bean.setScItemRootCategoryName("未知分类");
            }
            return bean;
        }).collect(Collectors.toList());
        return PageResult.of(result,response.getTotal());
    }

    @Override
    public List<MonthSalesStockReportSDO> statisticalQuery(QueryMonthStockReportRequest request) {
        MonthSalesStockReportLoadListVO listVO = loadListVORequestConverter(request);
        Result<List<MonthSalesStockReport>> response = monthSalesStockReportService.statisticalQuery(listVO);
        List<MonthSalesStockReportSDO> result = response.getResult().stream().map(x -> {
            MonthSalesStockReportSDO bean = new MonthSalesStockReportSDO();
            BeanUtils.copyProperties(x,bean);
            bean.setScItemRootCategoryId(StringUtils.isNotBlank(x.getScItemRootCategoryId())?x.getScItemRootCategoryId():"-1");
            return bean;
        }).collect(Collectors.toList());
        return result;
    }

    private MonthSalesStockReportLoadListRequest loadListRequestConverter(QueryMonthStockReportRequest request){
        MonthSalesStockReportLoadListRequest listVO = MonthSalesStockReportLoadListRequest.newMonthSalesStockReportLoadListRequestBuilder()
                .orgBusinessUnitIds(request.getOrgBusinessUnitIds())
                .statisticalDate(request.getStatisticalDate())
                .dataMark(request.getDataMark())
                .build();
        listVO.setStart(request.getStart());
        listVO.setLimit(request.getLimit());
        return listVO;
    }


    private MonthSalesStockReportLoadListVO loadListVORequestConverter(QueryMonthStockReportRequest request){
        MonthSalesStockReportLoadListVO listVO = MonthSalesStockReportLoadListVO.newMonthSalesStockReportLoadListVOBuilder()
                .orgBusinessUnitIds(request.getOrgBusinessUnitIds())
                .scItemIds(request.getScItemIds())
                .scItemCodes(request.getScItemCodes())
                .scItemRootCategoryIds(request.getScItemRootCategoryIds())
                .scItemGuaranteePeriodType(request.getScItemQuaranteePeriodType())
                .statisticalDate(request.getStatisticalDate())
                .groupBy(request.getGroupBy())
                .dataMark(request.getDataMark())
                .build();
        listVO.setStart(request.getStart());
        listVO.setLimit(request.getLimit());
        return listVO;
    }
}
